#include <iostream>
#include <string>
#include <cerrno>  // errno.h
#include <cstring> // string.h
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
const char *fifo_name = "/home/zio/pipe/aim";
void read_pipe(){
    int res =0;
    if (access(fifo_name, F_OK) == -1)
    {
        res = mkfifo(fifo_name, 0777);
        if (res != 0)
        {
            fprintf(stderr, "Could not create fifo %s\n", fifo_name);
            exit(EXIT_FAILURE);
        }
    }
    int pipe_fd = open(fifo_name, O_RDONLY);
    char save_read [1024];
   ssize_t n = read(pipe_fd ,save_read, sizeof(save_read) - 1);
  if(n>0){
            save_read[n]='\0';
            std::cout<<"child::"<<save_read<<std::endl;
            }
            else if(n==0){
                std::cout<<"SON EXITED"<<std::endl;
            }
    return;
}
void write_pipe(){
    int res =0;
    if (access(fifo_name, F_OK) == -1)
    {
        // 管道文件不存在
        // 创建命名管道
        res = mkfifo(fifo_name, 0777);
        if (res != 0)
        {
            fprintf(stderr, "Could not create fifo %s\n", fifo_name);
            exit(EXIT_FAILURE);
        }
    }
    int pipe_fd = open(fifo_name, O_WRONLY);
    if(pipe_fd<0){
        printf("errno=%d\n",errno);
    }
     std::string write_1="I am process A";
      ssize_t n =write(pipe_fd, write_1.c_str(), write_1.size());
     std::cout<<"father send "<<n<<pipe_fd<<" "<<res<<std::endl;
    return;
}

/*
const std::string comm_path = "./myfifo";
#define DefaultFd -1
#define Creater 1
#define User 2
#define Read O_RDONLY
#define Write O_WRONLY
#define BaseSize 4096

class NamePiped
{
private:
    bool OpenNamedPipe(int mode)
    {
        _fd = open(_fifo_path.c_str(), mode);
        if (_fd < 0)
            return false;
        return true;
    }

public:
    NamePiped(const std::string &path, int who)
        : _fifo_path(path), _id(who), _fd(DefaultFd)
    {
        if (_id == Creater)
        {
            int res = mkfifo(_fifo_path.c_str(), 0666);
            if (res != 0)
            {
                perror("mkfifo");
            }
            std::cout << "creater create named pipe" << std::endl;
        }
    }
    bool OpenForRead()
    {
        return OpenNamedPipe(Read);
    }
    bool OpenForWrite()
    {
        return OpenNamedPipe(Write);
    }
    // const &: const std::string &XXX
    // *      : std::string *
    // &      : std::string & 
    int ReadNamedPipe(std::string *out)
    {
        char buffer[BaseSize];
        int n = read(_fd, buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }
    int WriteNamedPipe(const std::string &in)
    {
        return write(_fd, in.c_str(), in.size());
    }
    ~NamePiped()
    {
        if (_id == Creater)
        {
            int res = unlink(_fifo_path.c_str());
            if (res != 0)
            {
                perror("unlink");
            }
            std::cout << "creater free named pipe" << std::endl;
        }
        if(_fd != DefaultFd) close(_fd);
    }

private:
    const std::string _fifo_path;
    int _id;
    int _fd;
};b
*/
